'use strict';

angular.module('myApp.account_add', [])

.controller('account_addCtrl', ['$scope','$http','$location', 
    function($scope,$http,$location) {

	    $http.get('v1/account_cat').success(function(data){
	        $scope.cats = data.result['data'];
	          // console.log($scope.cats);
	    });
	    //添加删除类型
	    
		$("#new_button").on('click',function(){
			var key = $("#new_input").val();
			if (key != '') {
				var new_cat = {'name' : key};
			    $http.post('v1/account_cat',new_cat).success(function(data){
		            if(!data.error ){
				    $http.get('v1/account_cat').success(function(data){
				        $scope.cats = data.result['data'];
				          // console.log($scope.cats);
				    });
		            }else{
		                alert(data.message);
		            }
			    });
	   		};
		});
	    $scope.del = function(cat){
		    $http.delete('v1/account_cat/'+cat.id).success(function(data){
				if( !data.error ){
					$scope.cats.splice($scope.cats.indexOf(cat),1);
					alert("删除成功","success");
				}else{
					alert(data.message);
				}
		    });
    	
	    };

        $http.get('v1/teacher').success(function(data){
            $scope.model = data.result['data'];
             console.log($scope.model);
        });

	    $scope.submit = function(account){
	    	var val=$('input:radio[name="type"]:checked').val();
	    	console.log(val);
	    	if (val != null) {
            account.cost_teacher_id = $scope.m;
            account.create_teacher_id = $scope.n;	    		
	    	};
    	    $scope.tem = angular.copy(account);
			console.log(account);	     	    
		    $http.post('v1/account',$scope.tem).success(function(data){
                if( !data.error ){
                    alert("添加成功","success");
                	window.location.href = '#/account';
                }else{
                    alert(data.message);
                }
		    });
	    };

    // 日历
    $('.form-date').datetimepicker(
    {
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0,
        format: 'yyyy-mm-dd'
    });	$("new_button")
}]);